﻿using FineUI;
using SysManage.BLL;
using System;
using System.Text.RegularExpressions;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Forum;

namespace SysManage.Module.Forum
{
    public partial class ForumUpgradeSettingEdit : PageBase
    {
        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartyForumUpgradeSetting setting = DB.PartyForumUpgradeSettings.Find(id);
                if (setting == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                txtUpgradeLevel.Text = setting.UpgradeLevel.ToString();
                txtUpgradeScore.Text = setting.UpgradeScore.ToString();
            }
        }
        #endregion

        #region Events

        private bool SaveData()
        {
            if(!Regex.IsMatch(txtUpgradeLevel.Text.Trim(), @"\d"))
            {
                Alert.Show("等级只能是数字！", String.Empty);
                txtUpgradeLevel.Text = "";
                return false;
            }

            if(!Regex.IsMatch(txtUpgradeScore.Text.Trim(), "^([0-9]{1,}[.][0-9]*)$"))
            {
                Alert.Show("分数只能是小数或整数！", String.Empty);
                txtUpgradeScore.Text = "";
                return false;
            }

            string id = GetQueryIntValue("id");

            if (id != "")
            {
                //更新
                PartyForumUpgradeSetting setting = DB.PartyForumUpgradeSettings.Find(id);
                if (setting == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return false;
                }

                setting.UpgradeLevel = Convert.ToInt32(txtUpgradeLevel.Text.Trim());
                setting.UpgradeScore = Convert.ToDouble(txtUpgradeScore.Text.Trim());

                DB.SaveChanges();
            }
            else
            {
                PartyForumUpgradeSetting setting = new PartyForumUpgradeSetting();
                setting.ID = StringHelper.NewID;
                setting.UpgradeLevel = Convert.ToInt32(txtUpgradeLevel.Text.Trim());
                setting.UpgradeScore = Convert.ToDouble(txtUpgradeScore.Text.Trim());

                DB.PartyForumUpgradeSettings.Add(setting);

                DB.SaveChanges();
            }

            return true;
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            if(SaveData())
            {
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }
        }
        #endregion
    }
}